package org.game.module.chatbo.testcmd;

import com.sojoys.artifact.core.IServer;
import com.sojoys.artifact.tools.ToolError;
import com.sojoys.artifact.tools.ToolList;
import com.xiaoleilu.hutool.date.DateUtil;
import com.xiaoleilu.hutool.util.MapUtil;
import org.cfg.cache.DiscreteDataCfgCache;
import org.constant.DiscreteDataID;
import org.constant.DiscreteDataKey;
import org.error.GameErrorCode;
import org.game.cache.OnlinePlayerCache;
import org.game.db.bean.Player;
import org.game.db.bean.Role;
import org.game.log.builder.LogDsp;
import org.game.log.dsp.GoodsDsp;
import org.game.manager.ItemManager;
import org.game.module.datingbo.DatingBO;
import org.game.module.datingbo.trigger.CityDatingTriggerManager;
import org.game.module.rolebo.RoleBO;
import org.game.protobuf.c2s.C2SDatingMsg;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by fxf on 2018-3-21.
 */
public class DatingTest {

    static DatingBO bo = IServer.getModules().getModule(DatingBO.classCode, DatingBO.class);

    public static byte[] dispatcher(Player player, List<String> cmds) {
        int index = ToolList.getInt(1, cmds);
        byte[] result = new byte[0];
        switch (index) {
            case 1:
                result = addFavor(player, ToolList.getInt(2, cmds));
                break;
            case 2:
                result = dailyDating(player, cmds);
                break;
            case 3:
                result = addCityDating(player, cmds);
                break;
            case 4:
                result = addSpecialCityDating(player, cmds);
                break;
            case 5:

                break;
        }
        return result;
    }

    public static byte[] dailyDating(Player player, List<String> cmds) {
        C2SDatingMsg.GetScriptMsg.Builder builder = C2SDatingMsg.GetScriptMsg.newBuilder();
        builder.setBuildId(ToolList.getInt(2, cmds));
        builder.setRoleId(ToolList.getInt(3, cmds));
        builder.setScriptType(C2SDatingMsg.ScriptType.valueOf(ToolList.getInt(4, cmds)));
        return bo.getScript(player.getPlayerProxy().getSession(), builder.build());
    }

    public static byte[] addCityDating(Player player, List<String> cmds) {
        Map<String, List<Integer>> triggerTimeFrame = CityDatingTriggerManager.getTimeFrame();
        Date now = new Date();
        int hour = DateUtil.thisHour(true);
        CityDatingTriggerManager.refreshCityDating(player, now);
        if (!CityDatingTriggerManager.getJustRefreshHour().contains(hour)) {
            CityDatingTriggerManager.trigger(player, triggerTimeFrame, now);
        }
        return new byte[0];
    }

    public static byte[] addSpecialCityDating(Player player, List<String> cmds) {
        int type = ToolList.getInt(2,cmds);
        List<Integer> triggerTypeList = (List<Integer>) DiscreteDataCfgCache.me().getData(DiscreteDataID.DATING, DiscreteDataKey.TRIGGER_ORDER);
        ToolError.isAndTrue(GameErrorCode.CLIENT_PARAM_IS_ERR,"城市约会类型不匹配",!triggerTypeList.contains(type));

        Map<String, List<Integer>> triggerTimeFrame = CityDatingTriggerManager.getTimeFrame();
        Date now = new Date();
        int hour = DateUtil.thisHour(true);
        CityDatingTriggerManager.refreshCityDating(player, now);
        if (!CityDatingTriggerManager.getJustRefreshHour().contains(hour)) {
            CityDatingTriggerManager.testTrigger(player, triggerTimeFrame, now, type);
        }
        return new byte[0];
    }

    public static byte[] addFavor(Player player, int favor) {
        Role role = player.getPlayerProxy().getCurrentRole();
        ItemManager.addGoodsAndPush(player, null, role, MapUtil.of(500007, favor), LogDsp.newBuilder(GoodsDsp.DATING_REWARD));
        return new byte[0];
    }
}
